package org.dromara.trace.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.trace.domain.bo.TraceShopBo;
import org.dromara.trace.domain.vo.TraceShopVo;
import org.dromara.trace.domain.TraceShop;
import org.dromara.trace.mapper.TraceShopMapper;
import org.dromara.trace.service.ITraceShopService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 店铺Service业务层处理
 *
 * @author Robin
 * @date 2024-12-05
 */
@RequiredArgsConstructor
@Service
public class TraceShopServiceImpl implements ITraceShopService {

    private final TraceShopMapper baseMapper;

    /**
     * 查询店铺
     *
     * @param shopId 主键
     * @return 店铺
     */
    @Override
    public TraceShopVo queryById(Long shopId){
        return baseMapper.selectVoById(shopId);
    }

    /**
     * 分页查询店铺列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 店铺分页列表
     */
    @Override
    public TableDataInfo<TraceShopVo> queryPageList(TraceShopBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TraceShop> lqw = buildQueryWrapper(bo);
        Page<TraceShopVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的店铺列表
     *
     * @param bo 查询条件
     * @return 店铺列表
     */
    @Override
    public List<TraceShopVo> queryList(TraceShopBo bo) {
        LambdaQueryWrapper<TraceShop> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TraceShop> buildQueryWrapper(TraceShopBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TraceShop> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getShopName()), TraceShop::getShopName, bo.getShopName());
        lqw.eq(StringUtils.isNotBlank(bo.getShopType()), TraceShop::getShopType, bo.getShopType());
        lqw.eq(StringUtils.isNotBlank(bo.getShopImg()), TraceShop::getShopImg, bo.getShopImg());
        lqw.eq(StringUtils.isNotBlank(bo.getShopAddress()), TraceShop::getShopAddress, bo.getShopAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getShopCoordinate()), TraceShop::getShopCoordinate, bo.getShopCoordinate());
        lqw.eq(StringUtils.isNotBlank(bo.getShopContacts()), TraceShop::getShopContacts, bo.getShopContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getShopTel()), TraceShop::getShopTel, bo.getShopTel());
        lqw.eq(StringUtils.isNotBlank(bo.getShopUrl()), TraceShop::getShopUrl, bo.getShopUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getShopDes()), TraceShop::getShopDes, bo.getShopDes());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TraceShop::getStatus, bo.getStatus());
        lqw.eq(bo.getOrderNum() != null, TraceShop::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getBaseId() != null, TraceShop::getBaseId, bo.getBaseId());
        lqw.eq(bo.getDeptId() != null, TraceShop::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, TraceShop::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增店铺
     *
     * @param bo 店铺
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TraceShopBo bo) {
        TraceShop add = MapstructUtils.convert(bo, TraceShop.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setShopId(add.getShopId());
        }
        return flag;
    }

    /**
     * 修改店铺
     *
     * @param bo 店铺
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TraceShopBo bo) {
        TraceShop update = MapstructUtils.convert(bo, TraceShop.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TraceShop entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除店铺信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
